package com.timoon.sample.web.user;


import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.timoon.component.panel.base.BaseService;
import com.timoon.component.panel.jqgrid.JqGridHelper;
import com.timoon.component.panel.jqgrid.JqGridParameter;
import com.timoon.component.panel.jqgrid.JqGridService;
import com.timoon.sample.dao.TUserDAO;
import com.timoon.sample.dao.UserRoleDAO;
import com.timoon.sample.dao.model.TUser;
import com.timoon.sample.dao.model.UserRole;
import com.timoon.sample.dao.model.UserRoleExample;

@Service("com.flt.sample.user.JqGridServiceImpl")
public class JqGridServiceImpl extends BaseService implements JqGridService {

	@Resource
	private TUserDAO tUserDAOImpl;
	
	@Resource
	private UserRoleDAO userRoleDAOImpl;

	@Override
	public String getGridData(HttpServletRequest request) {
		// TODO Auto-generated method stub
		
//		return "{\"total\":\"10\",\"page\":\"1\",\"records\":\"20\",\"rows\":[" +
//		"{\"invid\":\"1\",\"id\":\"1\",\"name\":\"付乐天\"}" +
//		"]}";
		
		JqGridParameter param=new JqGridParameter();
		JqGridHelper helper=new JqGridHelper();
		
		Map<String,String> paramMap=helper.coverParameterMap(request);
		param.setMap(paramMap);
		
		Integer records=tUserDAOImpl.countUserForPage(param);
		Integer rows=Integer.valueOf(paramMap.get("rows").toString());
		Integer page=Integer.valueOf(paramMap.get("page").toString());
		Integer startIndex=helper.getStartIndex(rows,page,records);
		
		param.setPage(page);
		param.setRows(rows);
		param.setStartIndex(startIndex);
		
		List<Map<String,Object>> maps=tUserDAOImpl.pageUser(param);
		
		return helper.getJqJSON(maps);
	}

	@Override
	public Integer countGridData(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	public TUserDAO gettUserDAOImpl() {
		return tUserDAOImpl;
	}

	public void settUserDAOImpl(TUserDAO tUserDAOImpl) {
		this.tUserDAOImpl = tUserDAOImpl;
	}

	@Override
	public void deleteByIds(String deleteIds) {
		// TODO Auto-generated method stub
		if(deleteIds!=null){
			String[] ids=deleteIds.split(",");
			for(String id:ids){
				this.tUserDAOImpl.deleteByPrimaryKey(Integer.valueOf(id));
			}
		}
	}


	@Override
	public TUser loadRecordById(String id) {
		// TODO Auto-generated method stub
		return this.tUserDAOImpl.selectByPrimaryKey(Integer.valueOf(id));
	}

	public UserRoleDAO getUserRoleDAOImpl() {
		return userRoleDAOImpl;
	}

	public void setUserRoleDAOImpl(UserRoleDAO userRoleDAOImpl) {
		this.userRoleDAOImpl = userRoleDAOImpl;
	}

	@Override
	public void saveOrUpdateRecord(Map<String, Object> map) {
		// TODO Auto-generated method stub
		final TUser user=(TUser) map.get("user");
		user.setStatus(1);
		
		Set<String> roleIds=(Set<String>) map.get("roleIds");		
		if(user.getId()==null){
			this.tUserDAOImpl.insert(user);
			
			if(roleIds.size()>0){
				//取出刚存的user，与role进行关联
//				TUser justUser=(TUser) this.tUserDAOImpl.selectByExample(new TUserExample(){
//					{
//						TUserExample.Criteria ct=this.createCriteria();
//						ct.andAccountEqualTo(user.getAccount());
//						ct.andNameEqualTo(user.getName());
//					}
//				}).get(0);
				
				//取出刚刚存入的user的id
				Integer justId=this.tUserDAOImpl.getIdByAccountName(user);
				//创建UserRole记录
				for(String roleId:roleIds){
					UserRole userRole=new UserRole();
					userRole.setRoleId(Integer.valueOf(roleId));
					userRole.setUserId(justId);
					this.userRoleDAOImpl.insert(userRole);
				}
			}
			
		}else{
			this.tUserDAOImpl.updateByPrimaryKey(user);
			
			if(roleIds.size()>0){
				//删除user-role之前的关联，根据传过来的roleIds重新关联(物理删除)
				this.userRoleDAOImpl.deleteByExample(new UserRoleExample(){
					{
						this.createCriteria().andUserIdEqualTo(user.getId());
					}
				});
				
				//创建UserRole记录
				for(String roleId:roleIds){
					UserRole userRole=new UserRole();
					userRole.setRoleId(Integer.valueOf(roleId));
					userRole.setUserId(user.getId());
					this.userRoleDAOImpl.insert(userRole);
				}
			}
		}
	}

}
